package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Game.*;
    import Message.Public.*;
    import flash.utils.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.*;
    import mortal.game.rules.*;
    import mortal.game.scene.events.*;
    import mortal.game.scene.map.*;
    import mortal.game.scene.modle.data.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.common.*;
    import mortal.game.view.hunt.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class HuntTaskController extends Controller
    {
        private var _huntAlert:HuntTipPanel;
        private var _nextMsgTime:uint;
        private var _lastDir:int;
        private var _huntAlertEd:Boolean;

        public function HuntTaskController()
        {
            
        }

        private function onTaskListRes(param1:Array) : void
        {
            var _loc_2:int = 0;
            var _loc_4:TaskInfo = null;
            var _loc_5:SProcess = null;
            var _loc_3:* = param1.length;
            while (_loc_2 < _loc_3)
            {
                
                _loc_4 = param1[_loc_2];
                if (_loc_4.sTask.type == ETaskType._ETaskTypeExplore && _loc_4.sTask.subType == ETaskSubType._ETaskSubTypeExploreTreasureHunt)
                {
                    _loc_5 = _loc_4.sTask.processes[0];
                    cache.huntTask.updateHunMapData(_loc_5.contents[0], _loc_5.contents[1], _loc_5.contents[2]);
                    cache.huntTask.inHuntDistance = false;
                    cache.huntTask.hasHuntTask = true;
                    if (_loc_4.status == ETaskStatus._ETaskStatusNotCompleted)
                    {
                        cache.huntTask.endHuntTask = false;
                        if (cache.huntTask.huntTaskInfo.mapId == MapFileUtil.mapID)
                        {
                            RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWalkEndHandler);
                            RolePlayer.instance.addEventListener(PlayerEvent.WALK_END, this.onGridWalkEndHandler);
                        }
                    }
                    else
                    {
                        cache.huntTask.endHuntTask = true;
                    }
                    break;
                }
                _loc_2++;
            }
            
        }

        private function onTaskGetRes(param1:Object) : void
        {
            var _loc_3:SProcess = null;
            if (param1 == null || param1.task == null)
            {
                
            }
            var _loc_2:* = param1.task as TaskInfo;
            if (_loc_2.sTask.type == ETaskType._ETaskTypeExplore && _loc_2.sTask.subType == ETaskSubType._ETaskSubTypeExploreTreasureHunt)
            {
                _loc_3 = _loc_2.sTask.processes[0];
                cache.huntTask.updateHunMapData(_loc_3.contents[0], _loc_3.contents[1], _loc_3.contents[2]);
                cache.huntTask.inHuntDistance = false;
                cache.huntTask.hasHuntTask = true;
                cache.huntTask.endHuntTask = false;
                if (cache.huntTask.huntTaskInfo.mapId == MapFileUtil.mapID)
                {
                    RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWalkEndHandler);
                    RolePlayer.instance.addEventListener(PlayerEvent.WALK_END, this.onGridWalkEndHandler);
                }
            }
            
        }

        private function onTaskEndRes(param1:Object) : void
        {
            if (param1 == null || param1.task == null)
            {
                
            }
            var _loc_2:* = param1.task as TaskInfo;
            if (_loc_2.sTask.type == ETaskType._ETaskTypeExplore && _loc_2.sTask.subType == ETaskSubType._ETaskSubTypeExploreTreasureHunt)
            {
                cache.huntTask.hasHuntTask = false;
                cache.huntTask.inHuntDistance = false;
                cache.huntTask.endHuntTask = true;
                RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWalkEndHandler);
                RolePlayer.instance.removeEventListener(PlayerEvent.WALK_END, this.onGridWalkEndHandler);
            }
            
        }

        private function onTaskCancelRes(param1:STask) : void
        {
            if (param1.type == ETaskType._ETaskTypeExplore && param1.subType == ETaskSubType._ETaskSubTypeExploreTreasureHunt)
            {
                cache.huntTask.hasHuntTask = false;
                cache.huntTask.inHuntDistance = false;
                cache.huntTask.endHuntTask = false;
                RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWalkEndHandler);
                RolePlayer.instance.removeEventListener(PlayerEvent.WALK_END, this.onGridWalkEndHandler);
            }
            
        }

        private function onTaskUpdate(param1:Array) : void
        {
            var _loc_2:int = 0;
            var _loc_4:TaskInfo = null;
            var _loc_3:* = param1.length;
            while (_loc_2 < _loc_3)
            {
                
                _loc_4 = param1[_loc_2];
                if (_loc_4.status == ETaskStatus._ETaskStatusHadCompleted && _loc_4.sTask.type == ETaskType._ETaskTypeExplore && _loc_4.sTask.subType == ETaskSubType._ETaskSubTypeExploreTreasureHunt)
                {
                    cache.huntTask.endHuntTask = true;
                    RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWalkEndHandler);
                    RolePlayer.instance.removeEventListener(PlayerEvent.WALK_END, this.onGridWalkEndHandler);
                    break;
                }
                _loc_2++;
            }
            
        }

        private function onSceneUpDateHandler(event:DataEvent) : void
        {
            if (cache.huntTask.hasHuntTask && !cache.huntTask.endHuntTask)
            {
                if (cache.huntTask.huntTaskInfo.mapId == MapFileUtil.mapID)
                {
                    this._huntAlertEd = false;
                    this.onGridWalkEndHandler(null);
                    RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWalkEndHandler);
                    RolePlayer.instance.addEventListener(PlayerEvent.WALK_END, this.onGridWalkEndHandler);
                }
                else
                {
                    cache.huntTask.inHuntDistance = false;
                    RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, this.onGridWalkEndHandler);
                    RolePlayer.instance.removeEventListener(PlayerEvent.WALK_END, this.onGridWalkEndHandler);
                }
            }
            
        }

        private function onGridWalkEndHandler(event:PlayerEvent) : void
        {
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:Array = null;
            var _loc_7:int = 0;
            if (SkillProgress.instance.running && SkillProgress.instance.type == ProcessType.Hunt)
            {
                SkillProgress.instance.dispose();
                RolePlayer.instance.huntPlayer.removeToRole();
            }
            var _loc_2:* = cache.huntTask.huntTaskInfo.px - RolePlayer.instance.currentPoint.x;
            var _loc_3:* = cache.huntTask.huntTaskInfo.py - RolePlayer.instance.currentPoint.y;
            if (_loc_3 == 0 && _loc_2 == 0)
            {
                MsgManager.addBroadCast("宝藏就在你的脚下！");
                if (!cache.huntTask.inHuntDistance)
                {
                    cache.huntTask.inHuntDistance = true;
                    _loc_4 = cache.task.updateHuntTaskProcess();
                    NetDispatcher.dispatchCmd(ServerCommand.TaskUpdate, _loc_4);
                }
                this._nextMsgTime = 0;
            }
            else
            {
                if (cache.huntTask.inHuntDistance)
                {
                    cache.huntTask.inHuntDistance = false;
                    _loc_6 = cache.task.updateHuntTaskProcess();
                    NetDispatcher.dispatchCmd(ServerCommand.TaskUpdate, _loc_6);
                }
                _loc_5 = _loc_2 * _loc_2 + _loc_3 * _loc_3;
                if (_loc_5 > 2500)
                {
                    if (this._huntAlertEd == false)
                    {
                        this._huntAlertEd = true;
                        MsgManager.addBroadCast("宝藏在距离你较远的地方");
                    }
                }
                else
                {
                    _loc_7 = GameMapUtil.getDirectionByXY(RolePlayer.instance.currentPoint.x, RolePlayer.instance.currentPoint.y, cache.huntTask.huntTaskInfo.px, cache.huntTask.huntTaskInfo.py);
                    this._lastDir = _loc_7;
                    if (_loc_5 >= 9 && _loc_5 <= 2500 && (this._lastDir == 0 || this._lastDir != _loc_7 || this._nextMsgTime == 0 || this._nextMsgTime <= getTimer()))
                    {
                        MsgManager.addBroadCast("宝藏就在你的" + DirectionType.getSide(_loc_7) + "方不远处");
                        this._nextMsgTime = getTimer() + 1000 * 1 * 5;
                    }
                    else if (_loc_5 >= 3 && _loc_5 < 9)
                    {
                        MsgManager.addBroadCast("宝藏就在你的" + DirectionType.getSide(_loc_7) + "方极近之处");
                        this._nextMsgTime = 0;
                    }
                    else if (_loc_5 >= 1 && _loc_5 < 3)
                    {
                        MsgManager.addBroadCast("宝藏就在你的九宫格范围内");
                        this._nextMsgTime = 0;
                    }
                }
            }
            
        }

        private function onItemUseHuntMap(event:DataEvent) : void
        {
            view.show();
            (view as IHuntTaskModule).updateData(cache.huntTask.huntTaskInfo);
            
        }

        private function onItemUseHuntShovel(event:DataEvent) : void
        {
            if (SkillProgress.instance.running && SkillProgress.instance.type == ProcessType.Hunt)
            {
                
            }
            var _loc_2:* = event.data as ItemData;
            GameProxy.huntProxy.startTreasureHunt(_loc_2.uid);
            
        }

        private function onHuntEndHandler() : void
        {
            RolePlayer.instance.huntPlayer.hurtEnd();
            setTimeout(this.hideHurt, 1000);
            GameProxy.huntProxy.endTreasureHunt();
            
        }

        private function hideHurt() : void
        {
            RolePlayer.instance.huntPlayer.removeToRole();
            
        }

        private function onStartHuntRes(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SStartTreasureHunt;
            if (_loc_2.flag)
            {
                RolePlayer.instance.huntPlayer.addToRole();
                RolePlayer.instance.huntPlayer.hurtStart();
                SkillProgress.instance.updateData(ProcessType.Hunt, "挖宝中", 5000, this.onHuntEndHandler, 2500, this.onIntervalHandler);
                if (RolePlayer.instance.hasMount)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.UnCallMounts));
                }
            }
            
        }

        private function onIntervalHandler() : void
        {
            RolePlayer.instance.huntPlayer.hurtShow();
            
        }

        private function onHuntUseItemReq(event:DataEvent) : void
        {
            var _loc_3:ItemData = null;
            if (SkillProgress.instance.running && SkillProgress.instance.type == ProcessType.Hunt)
            {
                
            }
            var _loc_2:* = cache.pack.backPackCache.getTypeItems(ECategory._ECategoryProp, EProp._EPropHuntShovel);
            if (_loc_2.length > 0)
            {
                _loc_3 = _loc_2[0];
                GameProxy.huntProxy.startTreasureHunt(_loc_3.uid);
            }
            else
            {
                MsgManager.addBroadCast("背包中没有铲子");
            }
            
        }

        public function showHuntAlert(param1:String) : void
        {
            if (!this._huntAlert)
            {
                this._huntAlert = new HuntTipPanel();
                this._huntAlert.layer = LayerManager.windowLayer;
            }
            this._huntAlert.update(param1);
            if (this._huntAlert.isHide)
            {
                this._huntAlert.show();
            }
            
        }

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.onTaskGetRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskEndRes, this.onTaskEndRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskCancelRes, this.onTaskCancelRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskListRes, this.onTaskListRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.onTaskUpdate);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateStartTreasureHunt, this.onStartHuntRes);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpDateHandler);
            Dispatcher.addEventListener(EventName.ItemUse_HuntMap, this.onItemUseHuntMap);
            Dispatcher.addEventListener(EventName.ItemUse_HuntShovel, this.onItemUseHuntShovel);
            
        }

        override protected function initView() : IView
        {
            var _loc_1:* = new HuntTaskModule();
            _loc_1.addEventListener(EventName.HuntUseItemReq, this.onHuntUseItemReq);
            return _loc_1;
        }

    }
}
